<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>房屋征收面积及补偿情况统计表</title>

    <link rel="stylesheet" href="../css/element.css">
    <style>

        table{
            border-collapse:collapse;
            border-spacing:0;
            margin-left: auto;
            margin-right: auto;
        }
        table td{
            /*width: 12.5%;*/
            /*height: 4rem;*/
            line-height: 20px;
            text-align: center;
            /*padding-left: 10px;*/
            /*padding-right: 10px;*/
        }
        table,tr,td{border:solid 1px;}
        table thead tr{border-color:rgba(255,255,255,0)}

        table td.title{
            background: #e7eaec;
        }
        table tbody tr[data-id]:hover {
            background-color: #edf5fa;
            cursor: pointer;
        }

        @media print {
            .not-print{ display:none;}
        }
    </style>
</head>
<body>
    <h3 style="text-align: center;">房屋征收面积及补偿情况统计表</h3>
    <table id="app">
        <thead>
            <tr>
                <td rowspan="3">序号</td>
                <td rowspan="3">项目名称</td>
                <td rowspan="3">征迁户姓名</td>
                <td rowspan="3">征迁涉及户数</td>
                <td colspan="4">征收土地情况（单位：㎡）</td>
                <td colspan="7">征收房屋情况（单位：㎡）</td>
                <td colspan="9">货币补偿情况（单位：万元）</td>
            </tr>
            <tr>
                <td rowspan="2">总土地面积</td>
                <td rowspan="2">埕空地面积</td>
                <td colspan="2">建筑占地面积</td>
                <td rowspan="2">总建筑面积</td>
                <td colspan="2">认定补偿安置建筑面积</td>
                <td colspan="4">认定不予补偿安置建筑面积</td>
                <td rowspan="2">总补偿款</td>
                <td colspan="2">认定补偿安置面积补偿款</td>
                <td colspan="4">认定不予补偿安置面积补偿款</td>
                <td rowspan="2">埕空地补偿款</td>
                <td rowspan="2">附属物补偿款</td>
            </tr>
            <tr>
                <td>认定补偿安置占地面积</td>
                <td>认定不予补偿安置占地面积</td>
                <td>货币补偿</td>
                <td>产权调换</td>
                <td>住宅</td>
                <td>临搭</td>
                <td>厂房</td>
                <td>寺庙</td>
                <td>货币补偿部分</td>
                <td>产权调换部分</td>
                <td>住宅</td>
                <td>临搭</td>
                <td>厂房</td>
                <td>寺庙</td>
            </tr>
        </thead>
        <tbody>
            <tr is="table-tr" v-for="(item, index) in items" :index="index" :household="item" :price="price" ></tr>
        </tbody>
    </table>

    <script src="../js/jquery.min.js?v=2.1.4"></script>
    <script src="../js/utils-tool.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../js/vue-resource.min.js"></script>
    <script src="../js/element.js"></script>

    <script>

        var digit = 2;
        function addition(num1,num2) {
            var m = Math.pow(10, digit);
            return parseInt((num1 * m + num2 * m), 10) / m;
        };
        function subtraction(num1,num2) {
            var m = Math.pow(10, digit);
            return parseInt((num1 * m - num2 * m), 10) / m;
        };
        function multiplication(num1,num2) {
            var m = Math.pow(10, digit);
            return parseInt(((num1 * m) * (num2 * m)), 10) / (m*m);
        };
        function division(num1,num2) {
            var m = Math.pow(10, digit);
            return (num1 * m) / (num2 * m);
        };


        Vue.http.options.emulateJSON = true;

        Vue.component('table-tr', {
            template: '<tr>\
                <td>{{ index+1 }}</td>\
                <td>{{ household.group.name }}</td>\
                <td>{{ household.house.name }}</td>\
                <td>{{ }}</td>\
                <td>{{ landTotal }}</td>\
                <td>{{ land.clearing.total }}</td>\
                <td>{{ land.building.admit }}</td>\
                <td>{{ land.building.disown }}</td>\
                <td>{{ house.total }}</td>\
                <td>{{ house.admit.currency.area }}</td>\
                <td>{{ house.admit.propertyRights.area }}</td>\
                <td>{{ house.disown.residential.area }}</td>\
                <td>{{ house.disown.temporary.area }}</td>\
                <td>{{ house.disown.workshop.area }}</td>\
                <td>{{ house.disown.temple.area }}</td>\
                <td></td>\
                <td>{{ house.admit.currency.compensation }}</td>\
                <td>{{ house.admit.propertyRights.compensation }}</td>\
                <td>{{ house.disown.residential.compensation }}</td>\
                <td>{{ house.disown.temporary.compensation }}</td>\
                <td>{{ house.disown.workshop.compensation }}</td>\
                <td>{{ house.disown.temple.compensation }}</td>\
                <td>{{ landClearingCompensation }}</td>\
                <td>{{ appendages }}</td>\
            </tr>',
            props: {
                index: Number,
                household: Object,
                price: Object
            },
            data: function() {
                return {
                    compensateMode: {},
                    land: {
                        clearing: {
                            total:0,
                            list:[]
                        },
                        building: {
                            admit: 0,
                            disown: 0
                        }
                    },
                    house: {
                        total: 0,
                        admit: {
                            currency: {// 货币
                                area: 0,
                                compensation: 0
                            },
                            propertyRights: { //产权调换
                                area: 0,
                                compensation: 0
                            }
                        },
                        disown: {
                            residential: {// 住宅
                                area: 0,
                                compensation: 0
                            },
                            temporary: {// 临搭
                                area: 0,
                                compensation: 0
                            },
                            workshop: { //厂房
                                area: 0,
                                compensation: 0
                            },
                            temple: {// 寺庙
                                area: 0,
                                compensation: 0
                            }
                        }
                    },
                    appendages: 0// 附属物总价
                }
            },
            created: function () {
                var that = this;
                this.initCompensateMode(function () {
                    that.initLand();
                    that.houseInit();
                });
                this.initAppendages();
            },
            computed: {
                total: function () {

                },
                landTotal: function () {
                    return addition(
                        addition(
                            this.land.building.admit,
                            this.land.building.disown),
                        this.land.clearing.total);
                },
                relocationAllowance: function () {
                    // 搬迁补助
                    var compensate = addition(this.house.admit.currency.area,this.house.admit.propertyRights.area);
                    var result = addition(
                        multiplication(compensate,this.price.relocation),
                        multiplication(this.house.admit.propertyRights.area,this.price.relocation)
                    );
                    if(result < 1000)
                        return 1000;
                    else
                        return result;
                },
                temporaryInterimAid: function () {
                    // 临时过渡补助

                    var result = 0;

                    var alreadyArea = 0;//记录已尽计算的面积
                    // 选择现房计算
                    if(this.compensateMode.existingCompensateArea > 0) {
                        var computedArea = 0;//要计算的面积
                        if (this.compensateMode.existingCompensateArea > this.house.admit.propertyRights.area) {
                            // 不合理情况，现房产权调换面积大于产权调换总面积
                            computedArea = this.house.admit.propertyRights.area
                        }else{
                            computedArea = this.compensateMode.existingCompensateArea;
                        }
                        var money = multiplication(multiplication(computedArea,this.price.temporary),6);
                        result = addition(result,money);

                        alreadyArea= addition(alreadyArea,computedArea)
                    }

                    // 选择一期计算
                    if(this.compensateMode.firstPhaseCompensateArea > 0) {
                        var computedArea = 0;//要计算的面积
                        var temp = addition(alreadyArea,this.compensateMode.firstPhaseCompensateArea)
                        if (temp > this.house.admit.propertyRights.area) {
                            // 不合理情况
                            computedArea = temp - this.house.admit.propertyRights.area
                        }else{
                            computedArea = this.compensateMode.existingCompensateArea;
                        }
                        var number = this.compensateMode.firstPhaseTimeLimit === null?12:this.compensateMode.firstPhaseTimeLimit;
                        var money = multiplication(multiplication(computedArea,this.price.temporary),number);
                        result = addition(result,money);

                        alreadyArea= addition(alreadyArea,computedArea)
                    }

                    // 选择二期计算
                    if(this.compensateMode.secondPhaseCompensateArea > 0) {
                        var computedArea = 0;//要计算的面积
                        var temp = addition(alreadyArea,this.compensateMode.secondPhaseCompensateArea)
                        if (temp > this.house.admit.propertyRights.area) {
                            // 不合理情况
                            computedArea = temp - this.house.admit.propertyRights.area
                        }else{
                            computedArea = this.compensateMode.secondPhaseCompensateArea;
                        }
                        var number = this.compensateMode.secondPhaseTimeLimit === null?36:this.compensateMode.secondPhaseTimeLimit;
                        var money = multiplication(multiplication(computedArea,this.price.temporary),number);
                        result = addition(result,money);

                        alreadyArea= addition(alreadyArea,computedArea)
                    }

                    // 获取为计算的产权调换面积，按照现房计算
                    var computedArea = subtraction(this.house.admit.propertyRights.area,alreadyArea);
                    // 货币补偿按照现房计算
                    computedArea = addition(computedArea,this.house.admit.currency.area);

                    var money = multiplication(multiplication(computedArea,this.price.temporary),6);
                    result = addition(result,money);

                    return result;

                },
                landClearingCompensation: function () {
                    var that = this;
                    var money = 0;
                    var reward = 0;
                    //土地计算
                    $(this.land.clearing.list).each(function (index,element) {
                        if(element.standard.pattern){
                            var temp = division(
                                multiplication(
                                    multiplication(element.area,that.price.compensate),
                                    element.standard.ratio),
                                100.0
                            );
                            money = addition(money,temp);
                        }else{
                            money = addition(money,multiplication(element.area,that.price.impose));
                            reward = addition(reward,element.area);
                        }
                    });
                    //奖励计算
                    money = addition(money,multiplication(reward,that.price.landReward));

                    if(!isNaN(money))
                        return money;
                },
                landBuildingCompensation: function () {
                    //建筑占地补偿款

                    var compensate = addition(this.house.admit.currency.area,this.house.admit.propertyRights.area);

                    var ratio = 0;
                    if(this.compensateMode.currencyCompensateArea == 0){
                        ratio = 0;
                    }else{
                        ratio = division(this.compensateMode.currencyCompensateArea,compensate)
                    }

                    // 认定的建筑占地补偿款
                    var admit = multiplication(
                        multiplication(this.land.building.admit,this.price.compensate),
                        ratio);
                    // 不予认定的建筑占地补偿款
                    var disown = multiplication(this.land.building.disown,this.price.compensate);
                    // 建筑占地奖励
                    var reward = multiplication(this.land.building.disown,this.price.landReward);

                    var result = addition(addition(admit,disown),reward);

                    if(!isNaN(result))
                        return result;
                }
            },
            methods: {
                initCompensateMode: function (methods) {
                    // 安置方式：选择产权调换面积、货币补偿面积
                    var that = this;
                    $.post(
                        "${path}/api/relocationTransition/get",
                        {masterId:this.household.house.id},
                        function (result) {
                            if(result.code == "${result_success}"){
                                if(result.data !== null){
                                    delete result.data.master;
                                    that.compensateMode = result.data;
                                }else{
                                    // 默认全部使用产权调换
                                    that.compensateMode = {currencyCompensateArea:0};
                                }
                                methods();
                            }else{
                                error(that).business(result.msg);
                            }
                        })
                },
                initLand: function () {
                    // 土地认定
                    var that = this;
                    $.post(
                        "${path}/api/identified/land/find",
                        {masterId:this.household.house.id},
                        function (result) {
                            if(result.code == "${result_success}"){
                                $(result.data.content).each(function (index,element) {
                                    that.landCompute(element);
                                });
                            }else{
                                error(that).business(result.msg);
                            }
                        });
                },
                landCompute: function (data) {
                    if (data.standard.type == "clearing") {

                        this.land.clearing.list.push(data);

                        this.land.clearing.total = addition(this.land.clearing.total,data.area);
                        return;
                    }
                    if (data.standard.type == "building") {
                        if(data.standard.pattern){
                            //按照比率计算

                            var compensate = division(
                                multiplication(data.area,data.standard.ratio),
                                100.0);
                            if(data.standard.formalities == 'without' && compensate > 150){
                                // 无手续，最大补偿面积不能大于150
                                compensate=150;
                            }
                            this.land.building.admit = addition(this.land.building.admit,compensate);
                            this.land.building.disown = addition(this.land.building.disown,subtraction(data.area,compensate));
                        }
                        return;
                    }
                },
                houseInit: function () {
                    var that = this;
                    $.post(
                        "${path}/api/identified/housing/find",
                        {masterId:this.household.house.id},
                        function (result) {
                            if(result.code == "${result_success}"){
                                $(result.data.content).each(function (index,element) {
                                    that.houseCompute(element);
                                });

                            }else{
                                error(that).business(result.msg);
                            }
                        });
                },
                houseCompute: function (data) {

                    this.house.total = addition(this.house.total,data.area);
                    if(data.landStandard.pattern){
                        //按照比率计算
                        var compensate = division(
                                multiplication(data.area,data.landStandard.ratio),
                                100.0);

                        if(data.landStandard.formalities == 'without' && compensate > 150){
                            //无手续最大补偿面积不能大于150
                            compensate = 150;
                        }
                        if(data.layer > 0 && data.atticHighly == true){
                            // 阁楼 且 层高小于2.2m
                            compensate = 0;
                        }
                        if(data.layer < 0) {
                            //地下室
                            if(data.basement=='Third2MoreThanHalf'){
                                //露出地面在其层高的1/3到1/2
                                compensate*=0.5;
                            } else if(data.basement=='LessThanThird'){
                                //露出地面在其层高的1/3一下
                                compensate*=0;
                            }
                        }

                        // 认定补偿安置建筑面积
                        var currency = this.compensateMode;
                        // 先添加到货币补偿
                        var cArea = addition(this.house.admit.currency.area, compensate);
                        if(cArea <= currency.currencyCompensateArea) {
                            // 如果小于等于选择的货币补偿，直接赋值
                            this.house.admit.currency.area = cArea;
                            // 计算货币补偿款
                            this.house.admit.currency.compensation = addition(
                                    this.house.admit.currency.compensation,
                                    this.houseCompensationCompute(compensate,data.decorateIntoNew,data.decorateLevel,data.structureIntoNew,data.structureLevel)
                                );

                        }else{
                            // 货币补偿面积直接等于选择的货币补偿面积
                            this.house.admit.currency.area = currency.currencyCompensateArea;
                            // 将cArea减去选择货币补偿的面积，就等于产权调换的面积
                            var residue = subtraction(cArea,currency.currencyCompensateArea);
                            // 将产权调换的面积加到总数中
                            this.house.admit.propertyRights.area = addition(this.house.admit.propertyRights.area,residue);

                            // 计算货币补偿款
                            this.house.admit.currency.compensation = addition(
                                this.house.admit.currency.compensation,
                                this.houseCompensationCompute(
                                    subtraction(compensate,residue),
                                    data.decorateIntoNew,
                                    data.decorateLevel,
                                    data.structureIntoNew,
                                    data.structureLevel)
                            );
                            // 计算产权调换补偿款
                            this.house.admit.propertyRights.compensation = addition(
                                this.house.admit.propertyRights.compensation,
                                this.houseCompensationCompute(
                                    residue,
                                    data.decorateIntoNew,
                                    data.decorateLevel)
                            );
                        }

                        // 认定不予补偿安置建筑面积
                        var disown = subtraction(data.area,compensate);
                        if(data.nature === "residential" || data.nature === "oldMan") {
                            // 住宅 ，老人会
                            this.house.disown.residential.area = addition(this.house.disown.residential.area,disown);
                            this.house.disown.residential.compensation = addition(
                                this.house.disown.residential.compensation,
                                this.houseCompensationCompute(
                                    disown,
                                    data.decorateIntoNew,
                                    data.decorateLevel,
                                    data.structureIntoNew,
                                    data.structureLevel)
                            );
                        } else if(data.nature === "temporary") {
                            // 临搭
                            this.house.disown.temporary.area = addition(this.house.disown.temporary.area, disown);
                            this.house.disown.temporary.compensation = addition(
                                this.house.disown.temporary.compensation,
                                this.houseCompensationCompute(
                                    disown,
                                    data.decorateIntoNew,
                                    data.decorateLevel,
                                    data.structureIntoNew,
                                    data.structureLevel)
                            );
                        } else if(data.nature === "workshop"){
                            // 厂房
                            this.house.disown.workshop.area = addition(this.house.disown.workshop.area,disown);
                            this.house.disown.workshop.compensation = addition(
                                this.house.disown.workshop.compensation,
                                this.houseCompensationCompute(
                                    disown,
                                    data.decorateIntoNew,
                                    data.decorateLevel,
                                    data.structureIntoNew,
                                    data.structureLevel)
                            );
                        } else if(data.nature === "temple"){
                            // 寺庙
                            this.house.disown.temple.area = addition(this.house.disown.temple.area,disown)
                            this.house.disown.temple.compensation = addition(
                                this.house.disown.temple.compensation,
                                this.houseCompensationCompute(
                                    disown,
                                    data.decorateIntoNew,
                                    data.decorateLevel,
                                    data.structureIntoNew,
                                    data.structureLevel)
                            );
                        }
                    }
                },
                houseCompensationCompute: function (area,decorateIntoNew,decorateLevel,structureIntoNew,structureLevel) {

                    var decorate = division(
                        multiplication(
                            area,
                            multiplication(decorateIntoNew.ratio,decorateLevel.price)
                        ),
                        100.0);
                    var structure = 0;
                    if(structureIntoNew !== undefined && structureLevel !== undefined){
                        structure = division(
                            multiplication(
                                area,
                                multiplication(structureIntoNew.ratio,structureLevel.price)
                            ),
                            100.0);
                    }
                    var result = addition(decorate,structure);
                    if(!isNaN(result))
                        return addition(decorate,structure);
                    else
                        return 0;
                },
                initAppendages: function () {
                    // 附属物
                    var that = this;
                    $.post(
                        "${path}/api/housing/appendages/find",
                        {masterId:this.household.house.id},
                        function (result) {
                            console.log(result)
                            if(result.code == "${result_success}"){
                                var total = 0;
                                $(result.data.content).each(function (index,element) {

                                });
                            }else{
                                error(that).business(result.msg);
                            }
                        })
                }
            }
        });

        new Vue({
            el: '#app',
            data: {
                items: [],
                price: {}
            },
            created: function () {
                this.initPrice();
                var that = this;
                this.$http.get("${path}/api/household/list/master").then(function (response) {
                    var result = response.body;
                    if(result.code == "${result_success}"){
                        that.items = result.data;
                    }else{
                        error(that).business(result.msg);
                    }
                },error(this).service);
            },
            methods: {
                initPrice: function () {
                    var that = this;
                    $.post(
                        "${path}/api/price/list",
                        function (result) {
                            if(result.code == "${result_success}"){
                                $(result.data).each(function (index,element) {
                                    that.price[element.type] = element.price;
                                })
                            }else{
                                error(that).business(result.msg);
                            }
                        });
                }
            }
        });
    </script>
</body>
</html>